一、原始服务的调用 通过网页的请求,这个网页请求到另一个网页,利用nginx做负载均衡。
消费者
1 2 3 4 5 6 7 8 9 10 @RestController public class IndexController { @Autowired RestTemplate restTemplate; @RequestMapping ("/indexClient.do" ) public String index () { ResponseEntity responseEntity = restTemplate.getForEntity("http://127.0.0.1/index.do" ,String.class); return responseEntity.getBody(); } }
RestTemplate :
是Spring用于同步client端的核心类,简化了与http服务的通信,并满足RestFul原则,程序代码可以给它提供URL,并提取结果。默认情况下,RestTemplate默认依赖jdk的HTTP连接工具。当然你也可以 通过setRequestFactory属性切换到不同的HTTP源,比如Apache HttpComponents、Netty和OkHttp。
生产者:1111
1 2 3 4 5 6 7 8 9 10 11 @RestController public class IndexController { @RequestMapping ("/index.do" ) public List> index() { List> list= new ArrayList(); Map map = new HashMap<>(); map.put("key" ,"port:1111" ); list.add(map); return list; } }
生产者:1121
1 2 3 4 5 6 7 8 9 10 11 @RestController public class IndexController { @RequestMapping ("/index.do" ) public List> index() { List> list= new ArrayList(); Map map = new HashMap<>(); map.put("key" ,"port:1121" ); list.add(map); return list; } }
nginx负载均衡
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 upstream backser { server 127.0.0.1:1111; server 127.0.0.1:1121; } server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; proxy_pass http://backser; } }
二、 服务注册中心eureka Eureka 是 Netflix 开发的,一个基于 REST 服务的,服务注册与发现的组件
它主要包括两个组件:Eureka Server 和 Eureka Client
Eureka Client:一个Java客户端,用于简化与 Eureka Server 的交互(通常就是微服务中的客户端和服务端)
Eureka Server:提供服务注册和发现的能力(通常就是微服务中的注册中心)
Eureka Server依赖包
1 2 3 4 <dependency > <groupId > org.springframework.cloudgroupId > <artifactId > spring-cloud-starter-netflix-eureka-serverartifactId > dependency >
Eureka Client依赖包
1 2 3 4 <dependency > <groupId > org.springframework.cloudgroupId > <artifactId > spring-cloud-starter-netflix-eureka-clientartifactId > dependency >
依赖管理
1 2 3 4 5 6 7 8 9 10 11 <dependencyManagement > <dependencies > <dependency > <groupId > org.springframework.cloudgroupId > <artifactId > spring-cloud-dependenciesartifactId > <version > ${spring-cloud.version}version > <type > pomtype > <scope > importscope > dependency > dependencies > dependencyManagement >
Server application.yml
1 2 3 4 5 6 7 8 9 10 11 12 server: port: 9000 eureka: server: enable-self-preservation: false instance: hostname: localhost client: register-with-eureka: false fetch-registry: false service-url: default-zone: http://${eureka.instance.hostname}:${server.port}/eureka
ServerApplication
1 2 3 4 5 6 7 @SpringBootApplication @EnableEurekaServer public class EurakeApplication { public static void main (String[] args) { SpringApplication.run(EurakeApplication.class, args); } }
client application.yml
1 2 3 4 5 6 7 8 9 server: port: 1111 eureka: client: service-url: defaultZone: http://127.0.0.1:9000/eureka spring: application: name: shadow-order-service-1111
ClientApplication
1 2 3 4 5 6 7 @SpringBootApplication @EnableDiscoveryClient public class OrderApplication1111 { public static void main (String[] args) { SpringApplication.run(OrderApplication1111.class, args); } }
三、dubbo zookeeper zookeeper服务器启动的逻辑: 1、初始化配置
2、监听端口
3、初始化DataTree
4、领导者选举