Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Acm #11

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open

Acm #11

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ hs_err_pid*
.idea/
*.iml
out/
*/target/
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<artifactId>nacos-spring-boot-config-example</artifactId>

<properties>
<nacos-config-spring-boot.version>0.2.0-RC1</nacos-config-spring-boot.version>
<nacos-config-spring-boot.version>0.2.1</nacos-config-spring-boot.version>
</properties>

<dependencies>
Expand All @@ -21,10 +21,21 @@
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-config-spring-boot-starter</artifactId>
<version>${nacos-config-spring-boot.version}</version>
</dependency>

<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-config-spring-boot-actuator</artifactId>
<version>${nacos-config-spring-boot.version}</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,22 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
* Document: https://help.aliyun.com/document_detail/94592.html
* <p>
* ACM 控制台添加配置:
* <p>
* Data ID:com.alibaba.nacos.example.properties
* <p>
* Group:DEFAULT_GROUP
* <p>
* 配置内容:connectTimeoutInMills=5000
*/
@SpringBootApplication
@NacosPropertySource(dataId = "example", autoRefreshed = true)
@NacosPropertySource(dataId = "com.alibaba.nacos.example.properties", autoRefreshed = true)
public class NacosConfigApplication {

public static void main(String[] args) {
SpringApplication.run(NacosConfigApplication.class, args);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.alibaba.nacos.example.spring.boot.controller;

import org.springframework.beans.factory.annotation.Value;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
Expand All @@ -11,16 +11,12 @@
@RequestMapping("config")
public class ConfigController {

@Value("${useLocalCache:false}")
private boolean useLocalCache;

public void setUseLocalCache(boolean useLocalCache) {
this.useLocalCache = useLocalCache;
}
@NacosValue(value = "${connectTimeoutInMills:5000}", autoRefreshed = true)
private int connectTimeoutInMills;

@RequestMapping(value = "/get", method = GET)
@ResponseBody
public boolean get() {
return useLocalCache;
public int get() {
return connectTimeoutInMills;
}
}
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
nacos.config.server-addr=127.0.0.1:8848
# \u5728\u547D\u540D\u7A7A\u95F4\u8BE6\u60C5\u5904\u53EF\u4EE5\u83B7\u53D6\u5230 endpoint \u548C namespace \u7684\u503C
nacos.config.endpoint=xxx
nacos.config.namespace=xxx

# \u63A8\u8350\u4F7F\u7528 RAM \u8D26\u6237\u7684 accessKey \u548C secretKey
nacos.config.access-key=xxx
nacos.config.secret-key=xxx

# endpoint http://localhost:8080/actuator/nacos-config
# health http://localhost:8080/actuator/health
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<artifactId>nacos-spring-boot-discovery-example</artifactId>

<properties>
<nacos-discovery-spring-boot.version>0.2.0-RC1</nacos-discovery-spring-boot.version>
<nacos-discovery-spring-boot.version>0.2.1</nacos-discovery-spring-boot.version>
</properties>

<dependencies>
Expand Down

This file was deleted.

5 changes: 4 additions & 1 deletion nacos-spring-boot-example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@

<dependencyManagement>
<dependencies>
<!-- Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>0.2.0.RELEASE</version>
<version>0.2.1.RELEASE</version>
</dependency>

</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
* @author xiaojing
* Document: https://help.aliyun.com/document_detail/94708.html
*/
@SpringBootApplication
public class NacosConfigApplication {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,25 @@
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import static org.springframework.web.bind.annotation.RequestMethod.GET;

@RestController
@RequestMapping("/config")
@RefreshScope
public class ConfigController {

@Value("${useLocalCache:false}")
private boolean useLocalCache;
@Value("${connectTimeoutInMills:5000}")
private int connectTimeoutInMills;

@RequestMapping("/get")
public boolean get() {
return useLocalCache;
/**
* http://localhost:8080/config/get
*/
@RequestMapping(value = "/get", method = GET)
@ResponseBody
public int get() {
return connectTimeoutInMills;
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
# \u5728\u547D\u540D\u7A7A\u95F4\u8BE6\u60C5\u5904\u53EF\u4EE5\u83B7\u53D6\u5230 endpoint \u548C namespace \u7684\u503C
spring.cloud.nacos.config.endpoint=xxx
spring.cloud.nacos.config.namespace=xxx

spring.application.name=example
# \u63A8\u8350\u4F7F\u7528 RAM \u8D26\u6237\u7684 accessKey \u548C secretKey
spring.cloud.nacos.config.access-key=xxx
spring.cloud.nacos.config.secret-key=xxx

# ACM \u63A7\u5236\u53F0\u6DFB\u52A0\u914D\u7F6E\uFF1A
# Data ID\uFF1Acom.alibaba.nacos.example.properties
# Group\uFF1ADEFAULT_GROUP
# \u914D\u7F6E\u5185\u5BB9\uFF1AconnectTimeoutInMills=5000
spring.application.name=com.alibaba.nacos.example
# \u6307\u5B9A\u914D\u7F6E\u7684\u540E\u7F00\uFF0C\u652F\u6301 properties\u3001yaml\u3001yml\uFF0C\u9ED8\u8BA4\u4E3A properties
spring.cloud.nacos.config.file-extension=properties
#spring.cloud.nacos.config.file-extension=yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>nacos-spring-cloud-example</artifactId>
<groupId>com.alibaba.nacos</groupId>
<version>0.2.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>nacos-spring-cloud-config-multi-data-ids-example</artifactId>

<dependencies>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>0.2.1.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.alibaba.nacos.example.spring.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
* @author xiaojing
*/
@SpringBootApplication
public class UserApplication {

@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(factory);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
return redisTemplate;
}

public static void main(String[] args) {
SpringApplication.run(UserApplication.class, args);
}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.alibaba.nacos.example.spring.cloud.controller;

import com.alibaba.nacos.example.spring.cloud.model.User;
import com.alibaba.nacos.example.spring.cloud.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("user")
public class UserController {

private final UserService userService;

@Autowired
public UserController(UserService userService) {this.userService = userService;}

/**
* http://localhost:8080/user?id=1
*/
@GetMapping
@ResponseBody
public User get(@RequestParam long id) {
return userService.findById(id);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.alibaba.nacos.example.spring.cloud.dao;

import com.alibaba.nacos.example.spring.cloud.model.User;
import org.springframework.data.repository.CrudRepository;

public interface UserRepository extends CrudRepository<User, Long> {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.alibaba.nacos.example.spring.cloud.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.io.Serializable;

/**
* CREATE TABLE `user` (
* `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
* `name` varchar(10) NOT NULL DEFAULT '',
* PRIMARY KEY (`id`)
* ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
*/
@Entity
public class User implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

private String name;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.alibaba.nacos.example.spring.cloud.service;

import com.alibaba.nacos.example.spring.cloud.model.User;
import org.springframework.stereotype.Service;

/**
* @author hexu.hxy
* @date 2019/1/7
*/

@Service
public interface UserService {

User findById(Long id);

}
Loading