spring cloud - 1
Posted xiaobin-hlj80
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring cloud - 1相关的知识,希望对你有一定的参考价值。
provider:
1. pom.xml
1) properties
<properties> ...... <spring-cloud.version>Finchley.SR1</spring-cloud.version> </properties>
2) dependencies
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zookeeper-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId> </dependency> <dependency>
3) dependencyManagement
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
2. config file
1) bootstrap.yml
#http://cloud.spring.io/spring-cloud-static/Finchley.SR1/multi/multi_spring-cloud-zookeeper-config.html
spring:
cloud:
zookeeper:
discovery:
instancePort: ${server.port}
instanceHost: localhost
#enabled: true
#register: true
connectString: 192.168.1.186:2181,192.168.1.187:2181,192.168.1.188:2181
2) application.properties
server.port=8080
spring.application.name=springcloud_zk
3. discovery client - ON
spring boot main class.
@EnableDiscoveryClient
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient //public class DemoApplication extends SpringBootServletInitializer { public class DemoApplication { // @Override // protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { // return application.sources(DemoApplication.class); // } public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
4. test info
1) autowired
@Autowired private DiscoveryClient discoveryClient;
2) println
// https://stackoverflow.com/questions/45085790/spring-cloud-discoveryclient-getlocalserviceinstance-deprecated-how-to-use-r discoveryClient.getServices().forEach(id -> { discoveryClient.getInstances(id).forEach(instance -> { System.out.println("/listPage, host:" + instance.getHost() + ", " + "service_id:" + instance.getServiceId()); }); });
all code:
@RequestMapping(value = "/listPage", method = RequestMethod.GET) public String findAll(Model model) { List<UserDetails> users = userService.getUsers(); model.addAttribute("users", users); for(UserDetails user : users) { System.out.println(user.getEmail() + " " + user.getName()); } // https://stackoverflow.com/questions/45085790/spring-cloud-discoveryclient-getlocalserviceinstance-deprecated-how-to-use-r discoveryClient.getServices().forEach(id -> { discoveryClient.getInstances(id).forEach(instance -> { System.out.println("/listPage, host:" + instance.getHost() + ", " + "service_id:" + instance.getServiceId()); }); }); redisCliTest(); redisTest(); return "userList"; }
3) zk info
#zkCli.sh
>ls /services/springcloud_zk >get /services/springcloud_zk/[tab key]
4) print info
http://localhost:8080/user/listPage
console:
/listPage, host:localhost, service_id:springcloud_zk
Reference:
1. 《SpringCloud之服务注册发现(Spring Cloud Zookeeper) |第十五章 -yellowcong》
以上是关于spring cloud - 1的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 Keycloak 保护 Angular 8 前端和使用网关、eureka 的 Java Spring Cloud 微服务后端
spring cloud - particle云架构代码结构