Eureka服務(wù)器集群請看這里 Spring Cloud Eureka+Spring Security 服務(wù)器集群
創(chuàng)建項(xiàng)目 user
第一步 修改 pom 文件
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR3</spring-cloud.version>
</properties>
<dependencies>
<!-- RabbitMQ 連接支持包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<!-- spring security 支持包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!-- jpa 支持包 jpa 類似 mybatis -->
<!--<dependency>-->
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-starter-data-jpa</artifactId>-->
<!--</dependency>-->
<!-- spring boot 引入的 web 開發(fā)的基礎(chǔ)依賴支持包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- mybatis 支持包 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
<!-- MyBatis Plus 支持包導(dǎo)入-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.0</version>
</dependency>
<!-- mysql 驅(qū)動(dòng)連接包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- lombok 插件支持 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- spring boot 單元測試依賴引入 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- rabbitMQ 單元測試依賴引入 -->
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit-test</artifactId>
<scope>test</scope>
</dependency>
<!-- spring security 單元測試依賴引入 -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Spring boot Actuator 監(jiān)控端點(diǎn) 支持包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- eureka 客戶端 依賴引入 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.1.3.RELEASE</version>
</dependency>
<!-- jwt -->
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.1</version>
</dependency>
<dependency>
<groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId>
<version>3.4.0</version>
</dependency>
<!-- spring session 管理依賴 -->
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-jdbc</artifactId>
</dependency>
</dependencies>
<!-- 引入 Spring cloud 的依賴-->
<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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
第二步 修改 Application.java 文件
@SpringBootApplication
@EnableEurekaClient//啟用 注冊 eureka 客戶端
public class MicroserviceSimplUserApplication {
@Bean//注冊 RestTemplate Bean
@LoadBalanced//加載 eureka 服務(wù)中心的 域表列名
public RestTemplate restTemplate(){
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(MicroserviceSimplUserApplication.class, args);
}
}
第三步 編寫文件 application.yml
server:
port: 8010
spring:
application:
name: microservice-consumer-movie
jpa:
generate-ddl: false
show-sql: true
hibernate:
ddl-auto: none
datasource:
url: jdbc:mysql://localhost:3306/springcloud?useSSL=false&serverTimezone=GMT%2B8&&characterEncoding=utf-8
username: root
password: 請?zhí)顚懩阕约旱拿艽a
driver-class-name: com.mysql.cj.jdbc.Driver
security:
user:
name: user
password: 123456
main:
allow-bean-definition-overriding: true
eureka:
client:
service-url:
defaultZone: http://user:123456@peer2:8762/eureka/,http://user:123456@peer1:8761/eureka/
instance:
prefer-ip-address: true #表示將自己的ip 注冊到 Eureka Server 。 默認(rèn)false ,表示注冊微服務(wù)所在 操作系統(tǒng)hostname 到 Eureka Server
metadata-map:
#自定義的元數(shù)據(jù), key/value 都可以隨便寫
my-metedata: 來自Movie的自定義元數(shù)據(jù)
haha: eureka
mybatis:
configuration:
map-underscore-to-camel-case: true
management:
endpoints:
web:
exposure:
include: "*" #暴露所有接口
endpoint:
shutdown.enabled: true #允許shutdown
health.show-details: always #展示詳細(xì)的health信息
jolokia:
enabled: false #允許 jolokia
config.debug: true
info: # Spring Boot Actuator info 端口信息配置 訪問 /info 時(shí)展示的數(shù)據(jù)
app: @project.artifactId@
encoding: @project.build.sourceEncoding@
java:
source: @java.version@
target: @java.version@
第四步 創(chuàng)建一個(gè)user服務(wù) 我這里是直接用mybatis-plus 做了一個(gè) 根據(jù)用戶id 查詢用戶的操作 controller 如下
@RestController
@Slf4j
public class UserController {
@Autowired
private RestTemplate restTemplate;
@Autowired
private IUserService userService;
@GetMapping("/{id}")
public UserEntity findById(@PathVariable Long id){
log.info("id:{}",id);
return userService.getById(id);
}
}
第五步啟動(dòng) eureka服務(wù)器 并啟動(dòng) user項(xiàng)目
訪問 http://peer1:8761 并登陸頁面
可以看到如下信息

user服務(wù)注冊后主頁.jpg
以上 一個(gè)服務(wù)提供者就算完事了 下面寫 服務(wù)消費(fèi)者
創(chuàng)建項(xiàng)目 movie
重復(fù) 上面一至二步
重復(fù)第三步 修改信息如下
server:
port: 8010
spring:
application:
name: microservice-consumer-movie
jpa:
generate-ddl: false
show-sql: true
hibernate:
ddl-auto: none
datasource:
url: jdbc:mysql://localhost:3306/springcloud?useSSL=false&serverTimezone=GMT%2B8&&characterEncoding=utf-8
username: root
password: 3sybseri5fb
driver-class-name: com.mysql.cj.jdbc.Driver
security:
user:
name: user
password: 123456
main:
allow-bean-definition-overriding: true
eureka:
client:
service-url:
defaultZone: http://user:123456@peer2:8762/eureka/,http://user:123456@peer1:8761/eureka/
instance:
prefer-ip-address: true #表示將自己的ip 注冊到 Eureka Server 。 默認(rèn)false ,表示注冊微服務(wù)所在 操作系統(tǒng)hostname 到 Eureka Server
metadata-map:
#自定義的元數(shù)據(jù), key/value 都可以隨便寫
my-metedata: 來自Movie的自定義元數(shù)據(jù)
haha: eureka
mybatis:
configuration:
map-underscore-to-camel-case: true
management:
endpoints:
web:
exposure:
include: "*" #暴露所有接口
endpoint:
shutdown.enabled: true #允許shutdown
health.show-details: always #展示詳細(xì)的health信息
jolokia:
enabled: false #允許 jolokia
config.debug: true
info: # Spring Boot Actuator info 端口信息配置 訪問 /info 時(shí)展示的數(shù)據(jù)
app: @project.artifactId@
encoding: @project.build.sourceEncoding@
java:
source: @java.version@
target: @java.version@
第六步 在 movie中 創(chuàng)建 一個(gè) 消費(fèi)服務(wù) 的controller 代碼如下
import com.bxr.microservicesimpleconsumermovie.business.movie.entity.UserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.client.support.BasicAuthenticationInterceptor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.List;
@RestController
public class MovieController {
@Autowired
private RestTemplate restTemplate;
@Autowired
private DiscoveryClient discoveryClient;
@GetMapping("/user/{id}")
public UserEntity findById(@PathVariable Long id){
//訪問被加密的路徑時(shí)需要用到username和password 下面這行代碼為restTemplate添加一個(gè)安全訪問攔截器
this.restTemplate.getInterceptors().add(new BasicAuthenticationInterceptor("user","123456"));
return this.restTemplate.getForObject("http://MICROSERVICE-PROVIDER-USER/"+id,UserEntity.class);
}
@GetMapping("/user-instance")
public List<ServiceInstance> showInfo(){
return this.discoveryClient.getInstances("microservice-provider-user");
}
}
啟動(dòng)服務(wù)后訪問 eureka集群服務(wù)其中的一個(gè)主頁可以看到如下信息

四全開eureka頁面.jpg
現(xiàn)在訪問 http ://192.168.10.250:8010/user/1 可以看到如下信息 注:不要直接復(fù)制地址 我加空格了 要不然他變成超鏈接。。
{
id: 1,
username: "account1",
name: "張三",
age: 20,
balance: 100
}
到此 eureka服務(wù)集群 注冊 調(diào)用已完成 。。。
我這個(gè)上面沒說 其實(shí) 我這個(gè) 集成了Spring Boot Actuator 可以直接查一下
還可以直接點(diǎn)擊 eureka服務(wù)器頁面中的url 可以看到如下信息
{
app: "microservice-provider-user",
encoding: "UTF-8",
java: {
source: "1.8.0_131",
target: "1.8.0_131"
}
}