微服务架构java
一、分布式服务组成的系统web
二、按照业务,而不是技术来划分组织spring
三、作有生命的产品而不是项目tomcat
五、自动化运维( DevOps )架构
六、高度容错性app
......负载均衡
以上是项目搭建目录,分别是eureka-server、spring-servic-9090、spring-servic-909一、spring-servic-9092。运维
一、新建EurekaApplication做为启动Eureka服务。maven
package appeurkaservic; import org.spring
framework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; /** * @Description * @Author ybb 34953126@qq.com * @Version V1.0.0 * @Since 1.0 * @Date 2019/10/21 */ @EnableEurekaServer @SpringBootApplication public class EurekaApplication { public static void main(String[] args) { SpringApplication.run(EurekaApplication.class,args); } }
二、配置application.yml分布式
registerWithEureka:因为该应用为注册中心,全部设置为false,表明不向注册中心注册本身。
fetchRegistry:因为注册中心的职责就是维护服务实例,它并不须要去检索服务,全部也设置为false。
server: port: 9999 eureka: instance: hostname: localhost client: registerWithEureka: false fetchRegistry: false serviceUrl: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka
三、添加依赖pom.xml
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <!--spring-cloud-eureka server --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> <version>1.4.4.RELEASE</version> </dependency> </dependencies> <!--添加springcloud--> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.SR1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
四、启动 EurekaApplication,以后访问http://localhost:9999/ ,端口在application.yml已经配置。
以后能够看到eureka信息面板。
一、目录
二、添加pom.xml依赖。
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-eureka-client --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> <version>1.4.4.RELEASE</version> </dependency> </dependencies> <!--添加springcloud--> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.SR1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
三、新建application9090
package com; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; /** * 端口号9090 * @Description * @Author ybb 34953126@qq.com * @Version V1.0.0 * @Since 1.0 * @Date 2019/10/20 */ @SpringBootApplication @EnableEurekaClient public class Application9090 { public static void main(String[] args) { SpringApplication.run(Application9090.class,args); } }
四、新建配置,配置服务Tomcat的端口号为9090
package com.config; import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * @Description * @Author ybb 34953126@qq.com * @Version V1.0.0 * @Since 1.0 * @Date 2019/10/20 */ @Configuration public class Appconfig { @Bean public ConfigurableServletWebServerFactory webServerFactory() { TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory(); factory.setPort(9090); return factory; } }
五、新建测试IndexController
package com.web; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * 微服务端口9090 * @Description * @Author ybb 34953126@qq.com * @Version V1.0.0 * @Since 1.0 * @Date 2019/10/20 */ @Controller public class IndexController { @RequestMapping("index.do") @ResponseBody public String index(){ return "服务端口号:9090"; } }
六、以后启动Application9090 访问 http://localhost:9090/index.do
七、 查看eureka 出现9090服务表明已经注册进来,其余两个是以前已经运行项目。
八、一样新建spring-srvice-9091项目,按spring-srvice-9090同样新建,修改相应的端口号便可。
一、pom.xml
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-eureka-client --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> <version>1.4.4.RELEASE</version> </dependency> </dependencies> <!--添加springcloud--> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.SR1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
二、Application9092
经过@EnableEurekaClient让该应用成为Eureka客户端应用。
package com; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; /** * @Description * @Author ybb 34953126@qq.com * @Version V1.0.0 * @Since 1.0 * @Date 2019/10/21 */ @SpringBootApplication @EnableEurekaClient public class Application9092 { public static void main(String[] args) { SpringApplication.run(Application9092.class,args); } }
三、Appconfig 在里面添加端口。
建立RestTemplate的spring Bean实例,并经过 @LoadBalanced 注解开启客户端负载均衡。
package com.config; import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; import org.springframework.boot.web.server.ConfigurableWebServerFactory; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate; /** * @Description * @Author ybb 34953126@qq.com * @Version V1.0.0 * @Since 1.0 * @Date 2019/10/21 */ @Configuration public class Appconfig { @Bean public ConfigurableWebServerFactory webServerFactory(){ TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory(); factory.setPort(9092); return factory; } @Bean @LoadBalanced public RestTemplate restTemplate(){ return new RestTemplate(); } }
四、测试IndexController
package com.web; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.client.RestTemplate; /** * @Description * @Author ybb 34953126@qq.com * @Version V1.0.0 * @Since 1.0 * @Date 2019/10/21 */ @Controller public class IndexController { @Autowired RestTemplate restTemplate; @RequestMapping("client.do") @ResponseBody public String index(){ ResponseEntity<String> result = restTemplate.getForEntity("http://microservice-srvic1/index.do",String.class); return result.getBody(); } }
application.yml
server: port: 9092 spring: application: name: microservice-9092-srvic3 eureka: client: serviceUrl: defaultZone: http://localhost:9999/eureka/
五、启动,spring-service-909一、spring-service-9092,查看spring eureka主页。显示以下表明三个服务注册成功。
六、访问spring-service-9092方法(http://localhost:9092/client.do)
便可随机访问9090和9091项目